/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.qihoo.gamecenter.sdk.support.systemmessage;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import com.qihoo.gamecenter.sdk.common.h.d.a;
import com.qihoo.gamecenter.sdk.common.i.d;
import com.qihoo.gamecenter.sdk.common.i.s;
import com.qihoo.gamecenter.sdk.support.systemmessage.a.c.a;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

public class b {
    private static SharedPreferences a = null;
    private static b b = null;
    private List c = new ArrayList();
    private Context d;
    private boolean e = false;
    private Handler f = new Handler() {
        public void handleMessage(Message paramMessage) {
            if (1 == paramMessage.what) {
                b.a(b.this, b.a(b.this));
            }
            else {
                if (2 != paramMessage.what)
                    return;
                b.b(b.this, b.a(b.this));
            }
        }
    };
    private com.qihoo.gamecenter.sdk.support.systemmessage.ui.a g = new com.qihoo.gamecenter.sdk.support.systemmessage.ui.a();
    private com.qihoo.gamecenter.sdk.support.systemmessage.ui.a.a h = new com.qihoo.gamecenter.sdk.support.systemmessage.ui.a.a() {
        public void a(a parama) {
            Message localMessage = new Message();
            localMessage.what = 2;
            b.a(b.this, localMessage, 3000L);
        }
    };
    private a i;
    private long j = -1L;
    private Set k = null;
    private Random l = null;
    private BroadcastReceiver m = null;
    private boolean n = false;
    private boolean o = false;
    private com.qihoo.gamecenter.sdk.support.systemmessage.a.a p = null;

    public static b a(Context paramContext) {
        if (null == b)
            b = new b(paramContext);
        return b;
    }

    private b(Context paramContext) {
        this.d = paramContext;
        this.g.a(this.h);
    }

    public void a(boolean paramBoolean) {
        this.n = paramBoolean;
        if (paramBoolean)
            b();
        else
            a();
    }

    public void a() {
        com.qihoo.gamecenter.sdk.support.g.a.a("SystemMessageMgr", new Object[] { "startQuerySysMsg Entry!" });
        c();
        j();
        if (this.n) {
            com.qihoo.gamecenter.sdk.support.g.a.a("SystemMessageMgr",
                    new Object[] { "startQuerySysMsg list showing return!" });
            return;
        }
        if (this.o) {
            com.qihoo.gamecenter.sdk.support.g.a.a("SystemMessageMgr",
                    new Object[] { "startQuerySysMsg already started return!" });
            return;
        }
        if (!(com.qihoo.gamecenter.sdk.support.c.a.a(this.d)))
            return;
        long l1 = 0L;
        if (this.j != -1L) {
            long l2 = System.currentTimeMillis() - this.j;
            long l3 = h();
            if (l2 < l3)
                l1 = l3 - l2;
        }
        b(false);
        Message localMessage = new Message();
        localMessage.what = 1;
        a(localMessage, l1);
        this.o = true;
    }

    public void b() {
        b(true);
    }

    public void a(Set paramSet) {
        if ((null == this.d) || (null == paramSet) || (paramSet.isEmpty()))
            return;
        int i1 = 0;
        StringBuilder localStringBuilder = new StringBuilder();
        Object localObject = paramSet.iterator();
        while (((Iterator) localObject).hasNext()) {
            String str1 = (String) ((Iterator) localObject).next();
            if (TextUtils.isEmpty(str1))
                continue;
            localStringBuilder.append(str1);
            localStringBuilder.append(",");
            if (i1 >= 15) {
                String str2 = localStringBuilder.toString();
                localStringBuilder.delete(0, localStringBuilder.length());
                a(this.d, str2);
            }
        }
        if (localStringBuilder.length() <= 0)
            return;
        localObject = localStringBuilder.toString();
        a(this.d, (String) localObject);
    }

    public boolean a(String paramString) {
        Set localSet = g();
        return localSet.contains(paramString);
    }

    private void b(Context paramContext) {
        if ((!(d.d(paramContext))) || (this.p != null)) {
            com.qihoo.gamecenter.sdk.support.g.a.a("SystemMessageMgr",
                    new Object[] { "doQuerySystemMessage net work not available or already send a query" });
            Message localMessage = new Message();
            localMessage.what = 1;
            long l1 = h();
            com.qihoo.gamecenter.sdk.support.g.a.a("SystemMessageMgr", new Object[] { "new query sysmsg interval = ",
                    Long.valueOf(l1) });
            a(localMessage, l1);
            return;
        }
        this.p = new com.qihoo.gamecenter.sdk.support.systemmessage.a.a(paramContext, new d.a(paramContext) {
            public void a(int paramInt, String paramString, com.qihoo.gamecenter.sdk.support.systemmessage.a.a.a parama) {
                com.qihoo.gamecenter.sdk.support.g.a.a("SystemMessageMgr",
                        new Object[] { "query recent msg resutl entry!" });
                b.a(b.this, System.currentTimeMillis());
                if (parama != null) {
                    localObject1 = parama.a();
                    Object localObject2;
                    if ((localObject1 != null) && (((List) localObject1).size() > 0)) {
                        b.a(b.this, true);
                        b.a(b.this, (List) localObject1);
                        localObject2 = new Message();
                        ((Message) localObject2).what = 2;
                        b.a(b.this, (Message) localObject2, 0L);
                        Intent localIntent = new Intent("action_receive_new_system_message");
                        this.a.sendBroadcast(localIntent);
                    }
                    if (parama.b()) {
                        localObject2 = new Intent("action_receive_new_service_message");
                        this.a.sendBroadcast((Intent) localObject2);
                    }
                }
                if (b.b(b.this).isEmpty()) {
                    b.a(b.this, false);
                    localObject1 = new Message();
                    ((Message) localObject1).what = 1;
                    long l = b.c(b.this);
                    com.qihoo.gamecenter.sdk.support.g.a.a("SystemMessageMgr", new Object[] {
                            "new query sysmsg interval = ", Long.valueOf(l) });
                    b.a(b.this, (Message) localObject1, l);
                }
                com.qihoo.gamecenter.sdk.support.g.a.a("SystemMessageMgr",
                        new Object[] { "check readed msg id cache!" });
                Object localObject1 = b.d(b.this);
                if ((localObject1 != null) && (!(((Set) localObject1).isEmpty())))
                    b.this.a((Set) localObject1);
                b.a(b.this, null);
            }
        });
        this.p.execute(new String[] { com.qihoo.gamecenter.sdk.common.a.c.c() });
    }

    private void c(Context paramContext) {
        com.qihoo.gamecenter.sdk.support.g.a.a("SystemMessageMgr", new Object[] { "doPopupMessageWindow Entry!" });
        a locala = f();
        if (locala != null) {
            this.g.a(paramContext, locala);
        }
        else {
            Message localMessage = new Message();
            localMessage.what = 1;
            localMessage.obj = paramContext;
            long l1 = System.currentTimeMillis() - this.j;
            long l2 = h();
            com.qihoo.gamecenter.sdk.support.g.a.a("SystemMessageMgr",
                    new Object[] { "pop msg wnd cost = ", Long.valueOf(l1), " config interval = ", Long.valueOf(l2) });
            if (l1 >= l2) {
                a(localMessage, 0L);
            }
            else {
                long l3 = l2 - l1;
                com.qihoo.gamecenter.sdk.support.g.a.a("SystemMessageMgr", new Object[] {
                        "next query sysmsg interval = ", Long.valueOf(l3) });
                a(localMessage, l3);
            }
        }
    }

    private void b(boolean paramBoolean) {
        com.qihoo.gamecenter.sdk.support.g.a.a("SystemMessageMgr", new Object[] { "doStopQuery Entry! clear = ",
                Boolean.valueOf(paramBoolean) });
        this.f.removeMessages(1);
        this.f.removeMessages(2);
        if (paramBoolean)
            e();
        this.o = false;
    }

    private void a(List paramList) {
        ArrayList localArrayList = new ArrayList();
        localArrayList.addAll(paramList);
        localArrayList.addAll(this.c);
        this.c.clear();
        int i1 = 0;
        Iterator localIterator = localArrayList.iterator();
        while (localIterator.hasNext()) {
            a locala = (a) localIterator.next();
            this.c.add(locala);
            if (++i1 >= 3)
                return;
        }
    }

    private void e() {
        this.c.clear();
    }

    private a f() {
        if (this.c.isEmpty())
            return null;
        a locala = (a) this.c.get(0);
        this.c.remove(0);
        return locala;
    }

    private void a(Context paramContext, String paramString) {
        new com.qihoo.gamecenter.sdk.support.systemmessage.a.c(paramContext, new d.a(paramString) {
            public void a(int paramInt, String paramString, c.a parama) {
                int i = 0;
                if ((parama != null) && (parama.a() == 0))
                    i = 1;
                if (i == 0)
                    b.a(b.this, this.a);
                else
                    b.b(b.this, this.a);
            }
        }).execute(new String[] { com.qihoo.gamecenter.sdk.common.a.c.c(), paramString });
    }

    private Set g() {
        com.qihoo.gamecenter.sdk.support.g.a.a("SystemMessageMgr", new Object[] { "loadCachedReadedMsgId Entry!" });
        if (this.k != null)
            return this.k;
        HashSet localHashSet = new HashSet();
        String str1 = d("readed_message_id_" + com.qihoo.gamecenter.sdk.common.a.c.c());
        com.qihoo.gamecenter.sdk.support.g.a.a("SystemMessageMgr", new Object[] {
                "loadCachedReadedMsgId load from file ids = ", str1 });
        if (!(TextUtils.isEmpty(str1))) {
            String[] arrayOfString1 = str1.split(",");
            if ((arrayOfString1 != null) && (arrayOfString1.length > 0))
                for (String str2 : arrayOfString1) {
                    if (TextUtils.isEmpty(str2))
                        continue;
                    localHashSet.add(str2);
                }
        }
        this.k = localHashSet;
        return this.k;
    }

    private void b(String paramString) {
        com.qihoo.gamecenter.sdk.support.g.a.a("SystemMessageMgr", new Object[] { "cacheReadedMsgId Entry! ids = ",
                paramString });
        if (TextUtils.isEmpty(paramString))
            return;
        String[] arrayOfString1 = paramString.split(",");
        if ((arrayOfString1 == null) || (arrayOfString1.length <= 0))
            return;
        Set localSet = g();
        int i1 = 0;
        for (String str : arrayOfString1) {
            if (TextUtils.isEmpty(str))
                continue;
            if (localSet.contains(str))
                continue;
            localSet.add(str);
            i1 = 1;
        }
        if (i1 == 0)
            return;
        b(localSet);
    }

    private void c(String paramString) {
        com.qihoo.gamecenter.sdk.support.g.a.a("SystemMessageMgr", new Object[] { "removeReadedMsgId entry! ids = ",
                paramString });
        if (TextUtils.isEmpty(paramString))
            return;
        String[] arrayOfString1 = paramString.split(",");
        if ((arrayOfString1 == null) || (arrayOfString1.length <= 0))
            return;
        Set localSet = g();
        int i1 = 0;
        for (String str : arrayOfString1) {
            if (!(localSet.contains(str)))
                continue;
            localSet.remove(str);
            i1 = 1;
        }
        if (i1 == 0)
            return;
        b(localSet);
    }

    private void b(Set paramSet) {
        if (null == paramSet)
            paramSet = new HashSet();
        StringBuilder localStringBuilder = new StringBuilder();
        Iterator localIterator = paramSet.iterator();
        while (localIterator.hasNext()) {
            String str = (String) localIterator.next();
            localStringBuilder.append(str);
            localStringBuilder.append(",");
        }
        this.k = paramSet;
        a("readed_message_id_" + com.qihoo.gamecenter.sdk.common.a.c.c(), localStringBuilder.toString());
    }

    private SharedPreferences d(Context paramContext) {
        if (null == a)
            a = paramContext.getSharedPreferences("qihoo_sdk_sysmsg_pre", 0);
        return a;
    }

    private String d(String paramString) {
        return d(this.d).getString(paramString, "");
    }

    private void a(String paramString1, String paramString2) {
        SharedPreferences.Editor localEditor = d(this.d).edit();
        localEditor.putString(paramString1, paramString2);
        localEditor.commit();
    }

    private long h() {
        long l1 = com.qihoo.gamecenter.sdk.support.c.a.c(this.d);
        long l2 = com.qihoo.gamecenter.sdk.support.c.a.b(this.d);
        i();
        long l3 = Math.max(1L, Math.abs(l1 - l2));
        long l4 = Math.abs(this.l.nextLong()) % l3;
        return (Math.min(l1, l2) + l4);
    }

    private void i() {
        if (this.l != null)
            return;
        int i1 = 0;
        try {
            String str = s.i(this.d);
            int i2 = str.length();
            int i3 = Math.min(i2, 6);
            i1 = Integer.parseInt(str.substring(str.length() - i3, str.length()));
        } catch (Exception localException) {}
        this.l = new Random(System.currentTimeMillis() + System.identityHashCode(this) + i1);
    }

    public void c() {
        com.qihoo.gamecenter.sdk.support.g.a.a("SystemMessageMgr", new Object[] { "regiterTaskOnTopReceiver entry!" });
        if (null != this.m)
            return;
        this.m = new BroadcastReceiver() {
            public void onReceive(Context paramContext, Intent paramIntent) {
                if (null == paramIntent)
                    return;
                String str = paramIntent.getAction();
                if ("action_app_on_top".equals(str)) {
                    if ((b.e(b.this)) || (b.f(b.this)))
                        return;
                    if (!(b.b(b.this).isEmpty())) {
                        Message localMessage = new Message();
                        localMessage.what = 2;
                        b.a(b.this, localMessage, 0L);
                    }
                    else {
                        b.this.a();
                    }
                }
                else if ("action_app_on_background".equals(str)) {
                    b.b(b.this, false);
                    b.g(b.this).a();
                }
                else {
                    if (!("action_matrix_destroyed".equals(str)))
                        return;
                    com.qihoo.gamecenter.sdk.support.g.a
                            .a("SystemMessageMgr", new Object[] { "=== Matix destory ===" });
                    com.qihoo.gamecenter.sdk.support.e.a.a(b.a(b.this)).a(false);
                    b.h(b.this);
                    b.b(b.this, true);
                }
            }
        };
        IntentFilter localIntentFilter = new IntentFilter();
        localIntentFilter.addAction("action_app_on_top");
        localIntentFilter.addAction("action_app_on_background");
        localIntentFilter.addAction("action_matrix_destroyed");
        this.d.registerReceiver(this.m, localIntentFilter);
    }

    public void d() {
        if (null == this.m)
            return;
        this.d.unregisterReceiver(this.m);
        this.m = null;
    }

    private void a(Message paramMessage, long paramLong) {
        this.f.removeMessages(paramMessage.what);
        this.f.sendMessageDelayed(paramMessage, paramLong);
    }

    private void j() {
        if (this.i == null)
            this.i = new a(null);
        if (this.i.isAlive())
            return;
        this.i.start();
    }

    private void k() {
        if (this.i == null)
            return;
        try {
            this.i.a();
            this.i = null;
        } catch (Exception localException) {
            com.qihoo.gamecenter.sdk.support.g.a.c("SystemMessageMgr", new Object[] { localException.toString() });
        }
    }

    private boolean l() {
        int i1 = 0;
        if (this.d != null) {
            ActivityManager localActivityManager = (ActivityManager) this.d.getSystemService("activity");
            List localList = localActivityManager.getRunningTasks(1);
            ActivityManager.RunningTaskInfo localRunningTaskInfo = (ActivityManager.RunningTaskInfo) localList.get(0);
            ComponentName localComponentName = localRunningTaskInfo.topActivity;
            String str1 = localComponentName.getPackageName();
            String str2 = localComponentName.getClassName();
            if ((this.d.getPackageName().equals(str1))
                    && (!("com.qihoo.gamecenter.sdk.activity.ContainerActivity".equals(str2)))
                    && (!(str2.contains("com.qihoopp.qcoinpay."))))
                i1 = 1;
        }
        return i1;
    }

    private class a extends Thread {
        private boolean b = false;

        public void run() {
            while ((!(this.b)) && (b.a(b.this) != null))
                while (true) {
                    Intent localIntent;
                    if (b.i(b.this)) {
                        localIntent = new Intent("action_app_on_top");
                        b.a(b.this).sendBroadcast(localIntent);
                    }
                    else {
                        localIntent = new Intent("action_app_on_background");
                        b.a(b.this).sendBroadcast(localIntent);
                    }
                    try {
                        Thread.sleep(1000L);
                    } catch (InterruptedException localInterruptedException) {
                        com.qihoo.gamecenter.sdk.support.g.a.c("SystemMessageMgr",
                                new Object[] { localInterruptedException.toString() });
                    }
                }
        }

        public void a() {
            this.b = true;
        }
    }
}